
<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            <el-table-column prop="building_name" label="写字楼名称" min-width="140px"/>
            <el-table-column prop="template_name" label="所属楼盘" min-width="140px"/>
            <el-table-column prop="district_label" label="区域板块" min-width="140px">
              <template slot-scope="scope">
                {{ scope.row.district_label ? scope.row.district_label.split('/')[scope.row.district_label.split('/').length - 1] : ''}}
              </template>
            </el-table-column>
            <el-table-column prop="publish_type" label="发布类型" min-width="100px">
            </el-table-column>
            <el-table-column prop="rent_type" label="租售方式" min-width="100px">
            </el-table-column>
            <el-table-column prop="building_type" label="写字楼类型" min-width="100px"/>
            <el-table-column prop="area" label="建筑面积">
              <template slot-scope="scope">
                {{ scope.row.area }}㎡
              </template>
            </el-table-column>
            <el-table-column prop="decoration" label="装修情况" min-width="100px"/>
            <el-table-column prop="followupUser" label="最后跟进人" width="120px"/>
            <el-table-column prop="can_register" label="最后跟进时间" width="160px">
              <template slot-scope="scope">
                <el-tag type="danger" v-if="scope.row.is_followup">{{ scope.row.followupTime }}</el-tag>
                <el-tag type="info" v-else>{{ scope.row.followupTime }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="160px">
              <template slot-scope="scope">
                <el-button type="text" @click="onFollowup(scope.row)">跟进记录</el-button>
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
    <Followup ref="FollowupRef" @get-page="initData" />
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";
import Followup from "./followup";

export default {
  name: "index",
  components: {
    Add,
    Followup
  },
  mixins: [crud],
  data() {
    return {
      request: http.officeBuilding,
      searchList: [
        {
          label: '所属楼盘',
          prop: 'template_name',
          name: 'input',
          clearable: true
        },
        {
          label: '写字楼名称',
          prop: 'building_name',
          name: 'input',
          clearable: true
        },
        {
          label: '发布类型',
          prop: 'publish_type',
          name: 'select',
          clearable: true,
          options: [
            {label: '全部', value: ''},
            {label: '传统写字楼', value: '传统写字楼'},
            {label: '联合办公', value: '联合办公'}
          ]
        },
        {
          label: '租售方式',
          prop: 'rent_type',
          name: 'select',
          clearable: true,
          options: [
            {label: '全部', value: ''},
            {label: '出租', value: '出租'},
            {label: '出售', value: '出售'}
          ]
        },
        {
          label: '区域板块',
          prop: 'district',
          name: 'input',
          clearable: true
        }
      ],
      searchParams: {
        template_name: '',
        building_name: '',
        publish_type: '',
        rent_type: '',
        district: ''
      },
      defaultParams: {},
      tableData: []
    }
  },
  methods: {
    formatTableData(data) {
      data.forEach(item => {
        let followList = item.followupList
        if (!followList.length) {
          item.is_followup = true
          item.followupTime = '-'
          item.followupUser = '-'
        }
        // 判断数据中最近一天时间距离今天是否超过5天
        if (followList.length > 0) {
          let sortList = followList.sort((a, b) => {
            return new Date(b.create_time) - new Date(a.create_time)
          })
          let lastTime = new Date(sortList[0].create_time)
          let nowTime = new Date()
          let day = Math.floor((nowTime.getTime() - lastTime.getTime()) / (24 * 3600 * 1000))
          if (day > 5) {
            item.is_followup = true
          } else {
            item.is_followup = false
          }
          item.followupTime = sortList[0].create_time
          item.followupUser = sortList[0].employee_name
        }
      })
    },
    onFollowup(row) {
      this.$refs.FollowupRef.open(row)
    }
  }
}
</script>

<style scoped lang="scss">
</style>
